package cn.iot.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.iot.common.dto.ResultMsg;
import cn.iot.sys.dao.SysClassroomDao;
import cn.iot.sys.entity.SysClassroom;
import cn.iot.sys.entity.SysDormitory;
import cn.iot.sys.service.SysClassroomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SysClassroomServiceImpl extends ServiceImpl<SysClassroomDao,SysClassroom> implements SysClassroomService {
    @Resource
    private SysClassroomDao sysClassroomDao;
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public ResultMsg getSysClassroomList(Map<String, Object> map) {
        logger.info("SysClassroomServiceImpl getSysClassroomList map{}",map);
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        String locationName = (String)map.get("locationName");
        if (StrUtil.isNotEmpty(locationName)){
            pageNum = 1;
            map.remove("pageNum");
            map.put("pageNum",pageNum);
        }
        int start = (pageNum-1)*pageSize;
        map.remove("pageSize");
        map.put("pageSize",pageSize);
        map.put("start",start);
        int count = sysClassroomDao.getSysClassroomCount(map);
        int pageCount;
        if (count>0){
            pageCount = count%pageSize==0? count/pageSize:count/pageSize+1;
        }else {
            pageCount = 0;
        }
        List<SysClassroom> sysClassroomList = sysClassroomDao.getSysClassroomList(map);
        map.put("sysClassroomList",sysClassroomList);
        map.put("pageCount",pageCount);
        ResultMsg msg = new ResultMsg(200,"成功",map);
        return msg;
    }

    @Override
    public ResultMsg addSysClassroom(SysClassroom sysClassroom) {
        logger.info("SysClassroomServiceImpl addSysClassroom sysClassroom{}",sysClassroom);
        int count = sysClassroomDao.addSysClassroom(sysClassroom);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg getSysClassroomCount(Map<String, Object> map) {
        logger.info("SysClassroomServiceImpl getSysClassroomCount map{}",map);
        int count = sysClassroomDao.getSysClassroomCount(map);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg getSysClassroomByDorId(int dormitoryId) {
        logger.info("SysClassroomServiceImpl getSysClassroomByDorId dormitoryId{}",dormitoryId);
        int count = sysClassroomDao.getSysClassroomByDorId(dormitoryId);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg updSysClassroom(SysClassroom sysClassroom) {
        logger.info("SysClassroomServiceImpl updSysClassroom sysClassroom{}",sysClassroom);
        int count = sysClassroomDao.updSysClassroom(sysClassroom);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg delClassroomById(int id) {
        logger.info("SysClassroomServiceImpl delClassroomById id{}",id);
        int count = sysClassroomDao.delClassroomById(id);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg getSysClassroomAll() {
        logger.info("SysClassroomServiceImpl getSysClassroomAll start ...");
        List<SysClassroom> sysClassroomList = this.baseMapper.selectList(null);
        ResultMsg msg = new ResultMsg(200,"成功",sysClassroomList);
        return msg;
    }
}
